﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BOHVote.BLL;
 
using BOHVote.Common;
using BOHVote.Entity;

namespace BOH.Information
{
    public partial class PersonalInfo : System.Web.UI.Page
    {
        BHUser user = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] != null)
            {
                user = (BHUser)Session["User"];
            }
            if (!IsPostBack)
            {
                if (user != null)
                {
                    user = (BHUser)Session["User"];
                    if (user.RightType == "U")
                    {
                        //DataSetApplyList(user.UnitID.Trim(), user.GovernID);
                        UnitDropBind(user.UnitID.Trim(), user.GovernID);
                    }
                    else if (user.RightType == "G")
                    {
                        //DataSetApplyList("-1", user.GovernID);
                        UnitGovernBind(user.GovernID);
                    }
                    else if (user.RightType == "C" || user.RightType == "A")
                    {
                        UnitGovernBind("");
                        //DataSetApplyList("-1", "-1");
                    }
                    //UserID = user.UserID;

                }
                else
                {

                }
                LoadDrop();
                //ApplyListDataSet();
            }
        }

        #region  <<  Apply Bind  >>
        public DataSet dstApplyList(string AppplyStyle,string UnitID, string GovernID, string ApplyName, string ApplyType, string SpecialityID, string SubjectID, string JobRank, string IDCard,string ApplyPeriod)
        {
            DataSet dst = null;
            string UnitGovrenID = string.Empty;
            dst = ApplyClass.dstApplys(AppplyStyle,UnitID, GovernID, ApplyName, ApplyType, SpecialityID, SubjectID, JobRank, IDCard, ApplyPeriod);
            return dst;
        }

        public void DataSetApplyList(string UnitID, string GovernID)
        {
            DataSet dst = dstApplyList("-1",UnitID, GovernID, string.Empty, "-1", "-1", "-1", "-1", string.Empty,(string)Session["ApplyPeriod"]);

            BindApplyList(dst);
        }

        public void ApplyListDataSet()
        {
            DataSet dst = new DataSet();
            if (user != null)
            {
                if (user.RightType == "C" || user.RightType == "A")
                {
                    dst = dstApplyList(drpApplyStyle.SelectedValue, drpUnit.SelectedValue, drpGovern.SelectedValue, txtApplyName.Text.Trim(), drpApplyType.SelectedValue, drpSpeciality.SelectedValue, drpSubject.SelectedValue, drpApplyJobRank.SelectedValue, txtIDCard.Text.Trim(), (string)Session["ApplyPeriod"]);
                    BindApplyList(dst);
                }
                else if (user.RightType == "G")
                {
                    if (drpGovern.SelectedValue != "-1")
                    {
                        dst = dstApplyList(drpApplyStyle.SelectedValue, drpUnit.SelectedValue, drpGovern.SelectedValue, txtApplyName.Text.Trim(), drpApplyType.SelectedValue, drpSpeciality.SelectedValue, drpSubject.SelectedValue, drpApplyJobRank.SelectedValue, txtIDCard.Text.Trim(), (string)Session["ApplyPeriod"]);
                        BindApplyList(dst);
                    }
                    else
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertError", "<script>alert('请选择主管局! ');</script>");
                    }
                }
                else if (user.RightType == "U")
                {
                    if (drpGovern.SelectedValue != "-1" || drpUnit.SelectedValue != "-1")
                    {
                        dst = dstApplyList(drpApplyStyle.SelectedValue, drpUnit.SelectedValue, drpGovern.SelectedValue, txtApplyName.Text.Trim(), drpApplyType.SelectedValue, drpSpeciality.SelectedValue, drpSubject.SelectedValue, drpApplyJobRank.SelectedValue, txtIDCard.Text.Trim(), (string)Session["ApplyPeriod"]);
                        BindApplyList(dst);
                    }
                    else
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertError", "<script>alert('请选择主管局和工作单位! ');</script>");
                    }
                }
                
            }
        }

        public void BindApplyList(DataSet dst)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
                if (AspNetPager1.RecordCount != 0)
                {
                    labNoRecord.Visible = false;
                    AspNetPager1.Visible = true;
                    PagedDataSource pds = new PagedDataSource();
                    pds.DataSource = dst.Tables[0].DefaultView;
                    pds.AllowPaging = true;
                    pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    pds.PageSize = AspNetPager1.PageSize;
                    ApplyViewList.DataSource = pds;
                    ApplyViewList.DataBind();
                }
                else
                {
                    ApplyViewList.DataSource = null;
                    ApplyViewList.DataBind();
                    AspNetPager1.Visible = false;
                    labNoRecord.Visible = true;
                }
            }
            else
            {
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
                ApplyViewList.DataSource = null;
                ApplyViewList.DataBind();
            }
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            ApplyListDataSet();
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            ApplyListDataSet();
        }
        #endregion


        #region <<  绑定查询条件选项  >>

        public void LoadDrop()
        {
            //SubjectBind();
            BindCurrentJobRank();
            BindApplyType();
            BindApplyStyle();
        }

        public void BindApplyStyle()
        {
            DataSet dst = OtherClass.dstGetApplyStyle();
            BindDropDownList(drpApplyStyle, dst);
        }

        protected void drpApplyStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            string level = drpApplyStyle.SelectedValue;
            SubjectBind(level);
            drpSpeciality.Items.Clear();
            ListItem li1 = new ListItem("请选择专业", "-1");
            this.drpSpeciality.Items.Add(li1);
        }

        public void BindApplyType()
        {
            DataSet dst = OtherClass.dstgetApplyType();
            BindDropDownList(drpApplyType, dst);
        }

        public void BindDropDownList(ListControl Controls, DataSet dst)
        {
            Controls.Items.Clear();
            ListItem list = new ListItem("请选择", "-1");
            Controls.Items.Add(list);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem itemlist = new ListItem(dr["dict_Display"].ToString(), dr["dict_Value"].ToString());
                Controls.Items.Add(itemlist);
            }
        }

        public void BindCurrentJobRank()
        {
            drpApplyJobRank.Items.Clear();
            ListItem li1 = new ListItem("请选择职称", "-1");
            drpApplyJobRank.Items.Add(li1);
            DataSet dst = OtherClass.getJobRank();
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["JobRankName"].ToString(), dr["JobRankID"].ToString());
                drpApplyJobRank.Items.Add(li);
            }
        }

        //public void BindPeriod()
        //{
        //    DataSet dst = PeriodClass.getAperiod();
        //    foreach (DataRow dr in dst.Tables[0].Rows)
        //    {
        //        ListItem li = new ListItem(dr["ApplyPeriodName"].ToString(), dr["ApplyPeriodID"].ToString());
        //        drpPeriod.Items.Add(li);
        //    }
        //}

        #region <<  绑定申请学科组和专业  >>


        public void SubjectBind(string level)
        {
            drpSubject.Items.Clear();
            DataSet dst = Subject.getSubjectByLevel(level);
            ListItem li1 = new ListItem("请选择学科组", "-1");
            this.drpSubject.Items.Add(li1);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                if (dr["SubjectID"].ToString().Length > 0)
                {
                    ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                    this.drpSubject.Items.Add(li);

                }
            }
        }

        protected void drpSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSubjectID = ((DropDownList)sender).SelectedValue;
            if (strSubjectID != "-1")
            {
                SpecialityBind(strSubjectID);
            }
            else
            {
                drpSpeciality.Items.Clear();
                ListItem li1 = new ListItem("请选择专业", "-1");
                this.drpSpeciality.Items.Add(li1);
            }
        }

        public void SpecialityBind(string SubjectID)
        {
            drpSpeciality.Items.Clear();
            ListItem li1 = new ListItem("请选择专业", "-1");
            this.drpSpeciality.Items.Add(li1);
            DataSet dst = Subject.getAllSpecialityBySubjectID(SubjectID);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["SpecialityName"].ToString(), dr["SpecialityID"].ToString());
                this.drpSpeciality.Items.Add(li);
            }
        }

        #endregion

        #region <<  主管局和工作单位绑定  >>

        public void UnitGovernBind(string GovernID)
        {
            DataSet dstGovern = null;
            ListItem list = new ListItem("请选择主管局", "-1");
            drpGovern.Items.Add(list);
            dstGovern = UnitClass.getGovernByGovernID(GovernID);

            if (dstGovern.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in dstGovern.Tables[0].Rows)
                {
                    if (row["GovernID"].ToString().Trim().Length != 0)
                    {
                        ListItem li = new ListItem(row["GovernName"].ToString(), row["GovernID"].ToString());
                        this.drpGovern.Items.Add(li);
                        //this.bindDropChildItem(row["GovernID"].ToString());
                    }
                }
            }
        }

        protected void drpGovern_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strGovernSelected = ((DropDownList)sender).SelectedValue;
            if (strGovernSelected != "-1")
            {
                string GovernID = strGovernSelected;
                bindDropChildItem(GovernID);
            }
            else
            {
                drpUnit.Items.Clear();
                ListItem list = new ListItem("请选择工作单位", "-1");
                drpUnit.Items.Add(list);
            }
        }

        public void bindDropChildItem(string GovernID)
        {
            drpUnit.Items.Clear();
            ListItem list = new ListItem("请选择工作单位", "-1");
            drpUnit.Items.Add(list);
            DataSet dstUnit = UnitClass.getUnitByGovern(GovernID);
            foreach (DataRow dr in dstUnit.Tables[0].Rows)
            {
                drpUnit.Items.Add(new ListItem(dr["UnitName"].ToString(), dr["UnitID"].ToString()));
            }
        }

        public void UnitDropBind(string UnitID, string GovernID)
        {
            string strUnitName = UnitClass.getUnitName(UnitID);
            ListItem li = new ListItem(strUnitName, UnitID);
            drpUnit.Items.Clear();
            drpUnit.Items.Add(li);
            string strGovernName = UnitClass.GetGovernName(GovernID);
            ListItem liGovern = new ListItem(strGovernName, GovernID);
            drpGovern.Items.Clear();
            drpGovern.Items.Add(liGovern);
        }

        #endregion
        #endregion

        public string encodeApplyID(string applyID)
        {
            string strEncode = string.Empty;
            strEncode = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(applyID)).Replace("+","%2B");
            return strEncode; 
        }
    }
}
